<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<cl-flex1 />
			<!-- 条件搜索 -->
			<cl-search ref="Search" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" >
				<template #slot-add-fenzhang-btn="{ scope }">
					<el-button
						v-if="!scope.row.zhongtianOrderNo"
						type="success"
						@click="
							() => {
								addFenzhang(scope.row);
							}
						"
						>添加分账</el-button
					>
				</template>
			</cl-table>
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" setup>
defineOptions({
	name: "order-merchant",
});

import { useCrud, useTable, useUpsert, useSearch } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { useI18n } from "vue-i18n";

const { service } = useCool();
const { t } = useI18n();

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: t("商户名称"),
			prop: "merchantName",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
			required: true,
		},
		// {
		// 	label: t("选择代理商中天系统"),
		// 	prop: "merchantId",
		// 	component: { name: "el-input", props: { clearable: true } },
		// 	span: 12,
		// },
		{
			label: t("代理商手机号"),
			prop: "merchantPhone",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
			required: true,
		},
		{
			label: t("商户描述"),
			prop: "merchantDescription",
			component: { name: "el-input", props: { type: 'textarea', rows: 4 } },
			span: 24,
		},
		{
			label: t("微信支付商务号"),
			prop: "merchantBusiness",
			component: { name: "el-input", props: { clearable: true } },
			span: 24,
		},
		{
			label: t("微信支付参数"),
			prop: "merchantPayParams",
			component: { name: "el-input", props: { type: 'textarea', rows: 4 } },
			span: 24,
		},
	],
	async onOpened() {
		if (Upsert.value.form.merchantPayParams) {
			Upsert.value.form.merchantPayParams = JSON.stringify(Upsert.value.form.merchantPayParams)
		}
	}
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ label: t("商户名称"), prop: "merchantName", minWidth: 120 },
		// { label: t("代理商中天系统ID"), prop: "merchantId", minWidth: 120 },
		{ label: t("代理商手机号"), prop: "merchantPhone", minWidth: 120 },
		{ label: t("微信支付商务号"), prop: "merchantBusiness", minWidth: 120 },
		{ label: t("商户描述"), prop: "merchantDescription", minWidth: 120 },
		{ label: t("微信支付参数"), prop: "merchantPayParams", minWidth: 120, showOverflowTooltip: true, },
		// {
		// 	label: t("创建时间"),
		// 	prop: "createTime",
		// 	minWidth: 170,
		// 	sortable: "desc",
		// 	component: { name: "cl-date-text" },
		// },
		// {
		// 	label: t("更新时间"),
		// 	prop: "updateTime",
		// 	minWidth: 170,
		// 	sortable: "custom",
		// 	component: { name: "cl-date-text" },
		// },
		{ type: "op", buttons: ["edit", "delete", "slot-add-fenzhang-btn"] },
	],
});

// cl-search
const Search = useSearch();

// cl-crud
const Crud = useCrud(
	{
		service: service.order.merchant,
	},
	(app) => {
		app.refresh();
	},
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}

async function addFenzhang(row) {
	const res = await service.order.merchant.addShareReceiver({
		merchant_id: row.id,
	})
	console.log(res)
}
</script>
